Placing a market order with stop loss and profit target

Oanda Endpoints

Oanda Order Definitions

OandapyV20 Order Documentation

@ July 2017


In [1]:
import pandas as pd
import oandapyV20
import oandapyV20.endpoints.orders as orders

In [2]:
accountID = ''
access_token = ''

In [3]:
client = oandapyV20.API(access_token=access_token)
r = orders.OrderList(accountID)
client.request(r)
store = []
# Check for current open orders
for oo in r.response['orders']:
    store.append(oo)
pd.DataFrame(store)


Out[3]:
createTime id instrument partialFill positionFill price replacesOrderID state stopLossOnFill timeInForce tradeID triggerCondition type units
0 2017-07-17T03:32:09.985720726Z 258 NaN NaN NaN 1.30000 NaN PENDING NaN GTC 256 DEFAULT STOP_LOSS NaN
1 2017-07-17T03:32:09.985720726Z 257 NaN NaN NaN 1.35000 NaN PENDING NaN GTC 256 DEFAULT TAKE_PROFIT NaN
2 2017-07-17T03:31:02.397646582Z 254 NaN NaN NaN 1.05000 NaN PENDING NaN GTC 252 DEFAULT STOP_LOSS NaN
3 2017-07-17T03:31:02.397646582Z 253 NaN NaN NaN 1.20000 NaN PENDING NaN GTC 252 DEFAULT TAKE_PROFIT NaN
4 2017-07-17T03:27:36.528339837Z 248 NaN NaN NaN 1.05000 NaN PENDING NaN GTC 247 DEFAULT STOP_LOSS NaN
5 2017-05-03T00:49:15.951393547Z 212 EUR_USD DEFAULT_FILL DEFAULT 1.20000 NaN PENDING {'price': '1.22000', 'timeInForce': 'GTC'} GTC NaN DEFAULT LIMIT -100
6 2017-05-02T11:00:34.663183537Z 187 EUR_USD DEFAULT_FILL DEFAULT 1.25000 174 PENDING NaN GTC NaN DEFAULT LIMIT -3300
7 2017-01-20T15:47:51.120880289Z 17 EUR_USD DEFAULT_FILL DEFAULT 1.25000 15 PENDING NaN GTC NaN DEFAULT LIMIT -500000
8 2017-01-20T15:47:33.998386716Z 13 EUR_USD DEFAULT_FILL DEFAULT 1.25000 11 PENDING NaN GTC NaN DEFAULT LIMIT -500000
9 2017-01-20T15:44:35.046525739Z 9 EUR_USD DEFAULT_FILL DEFAULT 1.20000 NaN PENDING {'price': '1.22000', 'timeInForce': 'GTC'} GTC NaN DEFAULT LIMIT -100

In [4]:
data = \
{
  "order": {
        "type": "MARKET",
        "units": "260",
        "instrument": "GBP_USD",
        "timeInForce": "FOK",
        "takeProfitOnFill": {
            "timeInForce": "GTC",
            "price": "1.35"
        },
        "stopLossOnFill": {
            "timeInForce": "GTC",
            "price": "1.30"
    }
  }
}

In [5]:
r = orders.OrderCreate(accountID, data=data)
client.request(r)


Out[5]:
{'lastTransactionID': '262',
 'orderCreateTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '259',
  'id': '259',
  'instrument': 'GBP_USD',
  'positionFill': 'DEFAULT',
  'reason': 'CLIENT_ORDER',
  'requestID': '24306955290395914',
  'stopLossOnFill': {'price': '1.30000', 'timeInForce': 'GTC'},
  'takeProfitOnFill': {'price': '1.35000', 'timeInForce': 'GTC'},
  'time': '2017-07-17T03:34:06.892037333Z',
  'timeInForce': 'FOK',
  'type': 'MARKET_ORDER',
  'units': '260',
  'userID': 5120068},
 'orderFillTransaction': {'accountBalance': '100006.6430',
  'accountID': '101-003-5120068-001',
  'batchID': '259',
  'commission': '0.0000',
  'financing': '0.0000',
  'fullPrice': {'asks': [{'liquidity': '10000000', 'price': '1.31016'}],
   'bids': [{'liquidity': '10000000', 'price': '1.30996'}],
   'closeoutAsk': '1.31041',
   'closeoutBid': '1.30971',
   'timestamp': '2017-07-17T03:34:04.628838365Z'},
  'id': '260',
  'instrument': 'GBP_USD',
  'orderID': '259',
  'pl': '0.0000',
  'price': '1.31016',
  'reason': 'MARKET_ORDER',
  'requestID': '24306955290395914',
  'time': '2017-07-17T03:34:06.892037333Z',
  'tradeOpened': {'tradeID': '260', 'units': '260'},
  'type': 'ORDER_FILL',
  'units': '260',
  'userID': 5120068},
 'relatedTransactionIDs': ['259', '260', '261', '262']}

In [6]:
client = oandapyV20.API(access_token=access_token)
r = orders.OrderList(accountID)
client.request(r)
store = []
# Check for current open orders
for oo in r.response['orders']:
    store.append(oo)

In [7]:
pd.DataFrame(store)[0:2]


Out[7]:
createTime id instrument partialFill positionFill price replacesOrderID state stopLossOnFill timeInForce tradeID triggerCondition type units
0 2017-07-17T03:34:06.892037333Z 262 NaN NaN NaN 1.30000 NaN PENDING NaN GTC 260 DEFAULT STOP_LOSS NaN
1 2017-07-17T03:34:06.892037333Z 261 NaN NaN NaN 1.35000 NaN PENDING NaN GTC 260 DEFAULT TAKE_PROFIT NaN

Notebook released under the Creative Commons Attribution 4.0 License.